<?php

class PhotoLibraryList extends DBList {
	protected $dbTable = "tblModPhotoLibrary";
	protected $rowClassType = "PhotoLibraryItem";
	
}

class PhotoLibraryItem extends DBListItem {
	
	public function __construct($data=null, $parent=null) {
		if(!(is_bool($parent) && $parent==false)) {
			parent::__construct($data, $parent);
		
			//Load Columns
			$l = new PhotoLibraryPhotoList($this);
			$this->customdata["photos"] = $l->getByFkPhotoLibrary($this->pkPhotoLibrary);
		}
		else {
			parent::__construct($data, null);
		}		
	}
	
	// Is called when the Item dont has a fkSecurityObject
	private function loadSecurityObject() {
		//Load Security Object from Instance
		
		$conn = MySQLConnection::getInstance();
		$fkSecurityObjet = $conn->quickQuery("SELECT fkSecurityObject FROM tblModuleInstance WHRERE pkModuleInstance = %", $this->rowdata["fkModuleInstance"]);
		
		$so = new SecurityObject();
		$so->loadSecurityObject($this->rowdata["fkSecurityObject"]);
		return $so;
		
	}
	
}	



class PhotoLibraryPhotoList extends DBList {
	
	protected $dbTable = "tblModPhotoLibraryPhoto";
	protected $rowClassType = "PhotoLibraryPhotoItem";
	
}

class PhotoLibraryPhotoItem extends DBListItem {
	
	protected $pkName = "pkPhoto";
	
	private $moduleName = "PhotoLibrary";
	
	public function getFilePath($type) {
		
		// Get fkModuleInstance
		if($this->getParentItem()) {
			$fkModuleInstance = $this->getParentItem()->fkModuleInstance;
		}
		if(!isset($fkModuleInstance)) {
			
			// Load Library to get Instance from there
			$library = new PhotoLibraryItem($this->fkPhotoLibrary, false);
			$fkModuleInstance = $library->fkModuleInstance;
		}
		
		$folder = str_pad($this->fkPhotoLibrary, 4, "0", STR_PAD_LEFT);
		$path = Runtime::getDataStore($fkModuleInstance, $folder) . "/" . $this->getFileName($type);
		
		return $path;
	}
	
	public function getFileName($type) {
		$filebase =  str_pad($this->pkPhoto, 9, "0", STR_PAD_LEFT);
		return $filebase . "_" . $type . ".jpg";
	}
	
	public function createPublicFile() {

		$publicfile = $this->getFilePath("public");
		$basefile = $this->getFilePath("view");
		
		if(!file_exists($publicfile)) {
			
			// Get fkModuleInstance
			$fkModuleInstance = $this->getParentItem()->fkModuleInstance;
			if(!$fkModuleInstance) {
				
				// Load Library to get Instance from there
				$library = new PhotoLibraryItem($this->fkPhotoLibrary, false);
				$fkModuleInstance = $library->fkModuleInstance;
			}
			
			$path = Runtime::getDataStore($fkModuleInstance);
					
			//Check the watermark-file
			$watermarkfile = $path . "watermark" . str_pad($fkModuleInstance, 4, "0", STR_PAD_LEFT) . ".png"; 
			
			if(file_exists($watermarkfile)) {
				
				//Photo
				$image = @imagecreatefromjpeg($basefile);
				$width = imagesx($image);
				$height = imagesy($image);
				
				//Watermark
				$watermark = imagecreatefrompng($watermarkfile);
				$w_width = imagesx($watermark);
				$w_height = imagesy($watermark);
				
				
				$ModuleInstance = new ModuleInstanceItem($fkModuleInstance);
				//Determine the Position
				$pos = Runtime::getSetting("watermark.position", $ModuleInstance->fkModule, $fkModuleInstance, "bottom-right");
				$offsetx = Runtime::getSetting("watermark.offsetx", $ModuleInstance->fkModule, $fkModuleInstance, 0);
				$offsety = Runtime::getSetting("watermark.offsety", $ModuleInstance->fkModule, $fkModuleInstance, 0);
				
				if($pos == "bottom-right") {
					$left = $width - $w_width - $offsetx;
					$top = $height - $w_height - $offsety;
				}
				elseif($pos == "bottom-left") {
					$left = $offsetx;
					$top = $height - $w_height - $offsety;
				}
				elseif($pos == "top-left") {
					$left = $offsetx;
					$top = $offsety;
				}
				elseif($pos == "top-right") {
					$left = $width - $w_width - $offsetx;
					$top = $offsety;
				}
				
				imagecopy($image, $watermark, $left, $top, 0,0, $w_width, $w_height);
				
				//Generate the file with the watermark
		        //$image = imagecreatetruecolor($width, $height);
		        
		        //imagealphablending($image,false);
				//$col=imagecolorallocatealpha($image,255,255,255,127);
				//imagefilledrectangle($image, 0, 0, $width, $height, $col);
				imagealphablending($image,false);
		        
				
		        //imagealphablending($image, false);
				//imagesavealpha($image, true);
		        	
				imagejpeg($image, $publicfile, 100);
			}
			else {
				copy($basefile, $publicfile);
			}
		}
				
	}
	
}



?>